package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;

    @Autowired
    private IAppointmentService appointmentService;


    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        //status
        statement.setStatus(Statement.STATUS_CONSUME);
        //创建时间
        statement.setCreateTime(new Date());

        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {

        Statement obj = statementMapper.selectByPrimaryKey(statement.getId());
        if(Statement.STATUS_PAID.equals(obj.getStatus())){
            throw new BusinessException("已经支付的结算单不允许修改");
        }
        //选择第二个
        statementMapper.updateByPrimaryKey(statement);

    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void updateAmount(Long id, BigDecimal total, BigDecimal amount, BigDecimal discountAmount) {
        statementMapper.updateAmount(id, total, amount, discountAmount);
    }

    @Override
    public void payStatement(Long statementId) {

        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        if(Statement.STATUS_PAID.equals(statement.getStatus())){
            throw  new BusinessException("请勿重复支付");
        }

        //改状态-已支付
        //收款时间   收款人
        statement.setPayTime(new Date());
        statement.setPayeeId(ShiroUtils.getUserId());
        statement.setStatus(Statement.STATUS_PAID);

        statementMapper.payStatement(statement);


    }

    @Override
    public Statement queryByAppointmentId(Long appointmentId) {
        return statementMapper.selectByAppointmentId(appointmentId);
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        Statement statement = new Statement();
        statement.setAppointmentId(appointmentId);
        //到店--创建结算单
        Appointment appointment = appointmentService.get(appointmentId);
        statement.setCustomerName(appointment.getCustomerName());
        statement.setCustomerPhone(appointment.getCustomerPhone());
        statement.setActualArrivalTime(appointment.getActualArrivalTime());
        statement.setLicensePlate(appointment.getLicensePlate());
        statement.setCarSeries(appointment.getCarSeries());
        statement.setInfo(appointment.getInfo());
        statement.setServiceType(appointment.getServiceType() + 0L);
        statement.setCreateTime(new Date());
        statement.setStatus(Statement.STATUS_CONSUME);

        statementMapper.insert(statement);
        //修改预约状态：结算单生成
        appointmentService.changeStatus(appointmentId, Appointment.STATUS_SETTLE);
        return statement;
    }
}
